<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view><uni-nav-bar title="验证码登录" :statusBar="true" :fixed="true"></uni-nav-bar></view>
		<!-- #endif -->
		<view class="code-login-box">
			<view class="code-login-box-top">
				<image :src="commonImg('msjimg.png')"></image>
				<view class="code-login-box-top-text">会员价结账</view>
			</view>
			<view class="code-login-box-center">
				<view class="code-login-box-center-phone">
					<view class="code-login-box-center-phone-86">+86</view>
					<input type="number" v-model="phone" placeholder="输入手机号码" class="code-login-box-center-phone-input" placeholder-style="color:#AAAAAA" />
				</view>
				<view class="code-login-box-center-password">
					<view class="code-login-box-center-password-left">
						<input type="number" v-model="messageCode" placeholder="输入验证码" placeholder-style="color:#AAAAAA" />
					</view>
					<view class="code-login-box-center-password-right">
						<view class="code-login-box-center-password-right-btn" :class="{ 'code-login-box-center-password-right-btn-active': timer !== null }" @click="getSendSms">
							{{ getMessageText }}
						</view>
					</view>
				</view>
				<view class="code-login-box-center-login">
					<button @click="messagelogin">登陆</button>
					<text @click="goLoginPage">使用账号密码登录</text>
				</view>
			</view>
			<view class="code-login-box-bottom">
				<view class="code-login-box-bottom-title">
					<view class="code-login-box-bottom-title-xianl"></view>
					<view class="code-login-box-bottom-title-text">快速登录</view>
					<view class="code-login-box-bottom-title-xianr"></view>
				</view>
				<view class="code-login-box-bottom-party-login"><image @click="loginByWeChat" :src="commonImg('wximg.png')"></image></view>
				<view class="code-login-box-bottom-agreement">
					<view>
						注册即同意
						<text>《用户协议》</text>
						和
						<text>《隐私策略》</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { config } from '@/utils/config.js';
import { myMixin } from '@/utils/mixin.js';
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue';

export default {
	mixins: [myMixin],
	components: {
		uniNavBar
	},
	data() {
		return {
			phone: '',
			messageCode: '',
			countdown: 60,
			getMessageText: '发送验证码',
			timer: null,
			codeId: '',
			code: ''
		};
	},
	// 页面卸载的时候，一定要清除定时器
	onUnload() {
		if (this.timer) {
			clearTimeout(this.timer);
		}
	},
	mounted() {
		// #ifdef H5
		let data = window.location.search.substr(1).split('&');
		console.log(data);
		data.forEach((ele, index) => {
			if (index == 0) {
				this.codeId = ele.substring(ele.indexOf('=') + 1);
			}
		});
		console.log(this.codeId);
		// #endif
	},
	onShow() {
		// #ifdef H5
		let code = this.getUrlParam('code');
		if (code) {
			console.log('code=' + code);
			uni.request({
				url: `${config.api_base_url}/member/member/getInfoByWeChatCode`,
				method: 'POST',
				data: {
					code: code
				},
				success: res => {
					let { status, data } = res.data;
					if (status.flag === true) {
						uni.setStorageSync('role', data.role);
						uni.setStorageSync('token', data.token);
						uni.setStorageSync('memberId', data.memberId);
						uni.showToast({
							title: '登录成功',
							icon: 'none'
						});
						uni.switchTab({
							url: '/pages/index/index'
						});
					} else {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					}
				}
			});
		} else {
			console.log('no code');
		}
		// #endif
	},

	methods: {
		loginByWeChat() {
			// #ifdef H5
			if (this.code === '') {
				//获取code
				let url = encodeURIComponent(window.location.href);
				window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6246e0503023027f&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
			}
			// #endif
			
		},
		
		// #ifdef H5
		getUrlParam(name) {
			let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
			let r = window.location.search.substr(1).match(reg);
			if (r != null) {
				return unescape(r[2]);
			}
			return null;
		},
		// #endif
		// 倒计时操作
		beginSetTime() {
			if (this.countdown === 0) {
				this.getMessageText = '发送验证码';
				this.countdown = 60;
				this.timer = null;
				return;
			} else {
				this.getMessageText = `${this.countdown}s后重发`;
				this.countdown--;
			}
			this.timer = setTimeout(() => {
				this.beginSetTime();
			}, 1000);
		},
		// 获取验证码按钮点击事件
		getSendSms() {
			if (this.timer) {
				return false;
			}
			const phone = this.phone;
			if (phone === '') {
				uni.showToast({
					title: '手机号码不能为空',
					icon: 'none'
				});
				return false;
			}
			if (!/^1[345678]\d{9}$/.test(phone)) {
				uni.showToast({
					title: '手机号码格式错误',
					icon: 'none'
				});
				return false;
			}
			this.beginSetTime();
			uni.request({
				url: `${config.api_base_url_tools}sms`,
				method: 'POST',
				data: {
					sender: 'member',
					phone: phone,
					flag: 'login'
				},
				success: res => {
					let { status } = res.data;
					if (status.flag === true) {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络异常, 无法获取验证码',
						icon: 'none'
					});
				}
			});
		},
		// 短信登录方法
		messagelogin() {
			const phone = this.phone;
			const messageCode = this.messageCode;
			if (phone === '') {
				uni.showToast({
					title: '手机号码不能为空',
					icon: 'none'
				});
				return false;
			}
			if (messageCode === '') {
				uni.showToast({
					title: '验证码不能为空',
					icon: 'none'
				});
				return false;
			}
			uni.showLoading({
				title: '加载中'
			});
			uni.request({
				url: `${config.api_base_url}member/member/login/phone/${messageCode}`,
				method: 'POST',
				header: {
					'Content-Type': 'application/json'
				},
				data: {
					phone: phone
				},
				success: res => {
					let { status, data } = res.data;
					if (status.flag === true) {
						uni.setStorageSync('role', data.role);
						uni.setStorageSync('token', data.token);
						uni.setStorageSync('memberId', data.memberId);
						uni.showToast({
							title: '登录成功',
							icon: 'none'
						});
						uni.switchTab({
							url: '../index/index'
						});
					} else {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				},
				complete: () => {
					uni.hideLoading();
				}
			});
		},
		//跳转到使用账号和密码登录页面
		goLoginPage() {
			uni.redirectTo({
				url: '../login/login'
			});
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
}
</style>
<style scoped>
.code-login-box {
	border-top: 2rpx solid rgba(0, 0, 0, 0.05);
	padding-bottom: 60rpx;
	box-sizing: border-box;
}

.code-login-box-top {
	padding-top: 88rpx;
	box-sizing: border-box;
}

.code-login-box-top image {
	width: 144rpx;
	height: 144rpx;
	display: block;
	margin: 0px auto;
}

.code-login-box-top-text {
	font-size: 28rpx;
	letter-spacing: 3rpx;
	line-height: 40rpx;
	color: rgba(36, 36, 36, 1);
	text-align: center;
	margin-top: 38rpx;
}

.code-login-box-center {
	padding: 130rpx 60rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-phone {
	border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
	display: flex;
	align-items: center;
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-phone-86 {
	height: 44rpx;
	font-size: 32rpx;
	font-weight: 600;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
	width: 140rpx;
}

.code-login-box-center-phone-input {
	height: 44rpx;
	font-size: 32rpx;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
}

.code-login-box-center-password {
	padding: 20rpx 0rpx;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.code-login-box-center-password-left {
	border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-password-left input {
	height: 44rpx;
	font-size: 32rpx;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
}

.code-login-box-center-password-right {
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-password-right-btn {
	line-height: 40rpx;
	height: 40rpx;
	font-size: 28rpx;
	width: 140rpx;
	background: #fff;
	border: none;
	padding: 0px;
	box-sizing: border-box;
}

.code-login-box-center-password-right-btn-active {
	color: #aaaaaa;
}

.code-login-box-center-login {
}

.code-login-box-center-login button {
	width: 482rpx;
	height: 84rpx;
	background: rgba(240, 34, 48, 1);
	border-radius: 42rpx;
	font-size: 28rpx;
	line-height: 84rpx;
	color: rgba(255, 255, 255, 1);
	margin: 108rpx auto 48rpx;
	letter-spacing: 4rpx;
}

.code-login-box-center-login text {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(36, 36, 36, 1);
	line-height: 36rpx;
	text-align: center;
	width: 100%;
	display: block;
	letter-spacing: 3rpx;
}

.code-login-box-bottom {
	margin-top: 60rpx;
}

.code-login-box-bottom-title {
	display: flex;
	justify-content: center;
	align-items: center;
}

.code-login-box-bottom-title-xianl {
	width: 284rpx;
	height: 2rpx;
	background: rgba(0, 0, 0, 0.05);
	margin-top: 2rpx;
}

.code-login-box-bottom-title-text {
	width: 120rpx;
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(170, 170, 170, 1);
	line-height: 36rpx;
	text-align: center;
}

.code-login-box-bottom-title-xianr {
	width: 284rpx;
	height: 2rpx;
	background: rgba(0, 0, 0, 0.05);
	margin-top: 2rpx;
}

.code-login-box-bottom-party-login {
}

.code-login-box-bottom-party-login image {
	width: 68rpx;
	height: 68rpx;
	display: block;
	margin: 46rpx auto 68rpx;
}

.code-login-box-bottom-agreement {
	text-align: center;
}

.code-login-box-bottom-agreement view {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(170, 170, 170, 1);
	line-height: 36rpx;
}

.code-login-box-bottom-agreement view text {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(102, 172, 255, 1);
	line-height: 36rpx;
}
</style>
